from requests_xml import XMLSession
import xml.etree.ElementTree as ET


def test_xml():
    # 设置session
    session = XMLSession()
    r = session.get('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')
    # print(r.text)
    # 可以拿到响应中所有的链接
    print(r.xml.links)
    # raw_xml返回字节形式的响应内容
    print(r.xml.raw_xml)
    #返回标签中的内容
    print(r.xml.text)
    #通过xpath
    item = r.xml.xpath("//link",first=True)
    print(item.text)

    #自己封装
    root = ET.fromstring(r.text)
    #查找根元素
    item = root.findall(".")
    print(item)
    items = root.findall(".//link")
    res = []
    for i in items:
        res.append(i.text)
    assert "xxx" not in items